<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\ShopKefu
 *
 * @property int $id
 * @property int $shop_id
 * @property int $uid
 * @property string $name
 * @property string $openid
 * @property int $available
 * @property string $created_at
 * @property-read \App\Models\Shop $shop
 * @property-read \App\Models\User $user
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShopKefu available()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShopKefu whereAvailable($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShopKefu whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShopKefu whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShopKefu whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShopKefu whereOpenid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShopKefu whereShopId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShopKefu whereUid($value)
 * @mixin \Eloquent
 */
class ShopKefu extends Model
{
    protected $table = 'shop_kefu';
    protected $primaryKey = 'id';
    protected $guarded = ['id'];

    public $timestamps = false;

    public static function boot()
    {
        parent::boot(); // TODO: Change the autogenerated stub
        static::creating(function ($model){
            $model->created_at = time();
        });
    }

    public function shop()
    {
        return $this->belongsTo(Shop::class, 'shop_id', 'shop_id');
    }

    public function user()
    {
        return $this->belongsTo(User::class, 'uid', 'uid');
    }

    /**
     * @param $query
     * @return mixed
     */
    public function scopeAvailable($query){
        return $query->where('available', 1);
    }
}
